<template>
  <div class="goodsinfo-container">
    <!-- 加入购物车小球 -->
    <transition @before-enter="beforeEnter" @enter="enter" @after-enter="afterEnter">
      <div class="ball" v-show="ballFlag" ref="ball"></div>
    </transition>

    <!-- 商品轮播图区域 -->
    <div class="mui-card">
      <div class="mui-card-content">
        <div class="lunbotu">
          <swiper :lunbotuList="lunbotu" :isfull="false"></swiper>
        </div>
      </div>
    </div>
    <!-- 商品购买参数 -->
    <div class="mui-card">
      <div class="mui-card-header">{{ info.title }}</div>
      <div class="mui-card-content">
        <div class="mui-card-content-inner">
          <p class="price">
            市场价 :
            <del>￥{{ info.market_price}}</del> &nbsp;&nbsp;销售价:
            <span class="now_price">￥{{ info.sell_price}}</span>
          </p>
          <p>
            购买数量:
            <numbox ref="numbox" @getcount="getSelectedCount" :max="max_selected"></numbox>
          </p>
          <p>
            <mt-button type="primary" size="small">立即购买</mt-button>
            <mt-button type="danger" size="small" @click="addToShopCar">加入购物车</mt-button>
          </p>
        </div>
      </div>
    </div>
    <!-- 商品参数区域 -->
    <div class="mui-card">
      <div class="mui-card-header">商品参数</div>
      <div class="mui-card-content">
        <div class="mui-card-content-inner">
          <p>商品货号: {{ info.goods_no }}</p>
          <p>库存情况: {{ info.stock_quantity }}</p>
          <p>上架时间: {{ info.add_time | dateFormat }}</p>
        </div>
      </div>
      <div class="mui-card-footer">
        <mt-button type="primary" size="large" plain @click="goDesc(id)">图文介绍</mt-button>
        <mt-button type="danger" size="large" plain @click="goComment(id)">商品评论</mt-button>
      </div>
    </div>
  </div>
</template>
<script>
import { Toast } from "mint-ui";
import swiper from "../subcomponents/swiper.vue";
import numbox from "../subcomponents/goodsinfo_numbox.vue";
export default {
  data() {
    return {
      id: this.$route.params.id,
      lunbotu: [], // 轮播图的数据
      info: {},
      ballFlag: false,
      selectedCount: 1,
      max_selected: 1,
    };
  },
  methods: {
    getLunBotu() {
      this.$http.get("api/getthumimages/" + this.id).then(result => {
        if (result.body.status === 0) {
          this.lunbotu = result.body.message;
        } else {
          Toast("获取数据失败");
        }
      });
    },
    getGoodsInfo() {
      this.$http.get("api/goods/getinfo/" + this.id).then(result => {
        if (result.body.status === 0) {
          this.info = result.body.message[0];
          this.max_selected = result.body.message[0].stock_quantity
        } else {
          Toast("获取失败");
        }
      });
    },
    goDesc(id) {
      // 点击使用编程时导航 , 跳转到图文介绍页面
      this.$router.push({ name: "goodsDesc", params: { id } });
    },
    goComment(id) {
      // 点击跳转到评论页面
      this.$router.push({ name: "goodsComment", params: { id } });
    },
    addToShopCar() {
      // 添加到购物车
      this.ballFlag = !this.ballFlag;
      let goodsinfo = {
        id: this.id,
        count: this.selectedCount,
        price: this.info.sell_price,
        max: this.max_selected,
        selected: true
      }
      // 调用方法 , 加入购物车
      this.$store.commit("addToCar", goodsinfo)
    },
    beforeEnter(el) {
      // const position = this.$refs.numbox.$refs.ballRef.getBoundingClientRect()
      // el.style.top = position.top + 10 + "px"
      // el.style.left = position.left + 10 + "px"
      el.style.transform = "translate(0, 0)";
    },
    enter(el, done) {
      // 1. 获取小球的 在页面中的位置
      const ballPosition = this.$refs.ball.getBoundingClientRect();
      const badgePostion = document
        .getElementById("badge")
        .getBoundingClientRect();

      const xDist = badgePostion.left - ballPosition.left
      const yDist = badgePostion.top - ballPosition.top
      
      el.offsetWidth;
      el.style.transform = `translate(${xDist}px, ${yDist}px)`;
      el.style.transition = "all 0.5s cubic-bezier(.4,-0.3,1,.68)";
      done();
    },
    afterEnter(el) {
      this.ballFlag = !this.ballFlag;
    },
    getSelectedCount(count) {
      // 当子组件把 选中的数量传递给父组件的时候 , 把选中的值保存到父组件的data 身上
      this.selectedCount = count
    }
  },  
  components: {
    swiper,
    numbox
  },
  created() {
    this.getLunBotu();
    this.getGoodsInfo();
  }
};
</script>
<style lang="scss" scoped>
.goodsinfo-container {
  background-color: #eee;
  overflow: hidden; // 取消 margin 塌陷
  .lunbotu {
    height: 200px;
  }

  .now_price {
    color: red;
    font-size: 16px;
  }

  .mui-card-footer {
    display: block;
    button {
      margin: 15px 0;
    }
  }
  .ball {
    width: 15px;
    height: 15px;
    border-radius: 50%;
    background-color: red;
    position: absolute;
    z-index: 99;
    top: 390px;
    left: 146px;
  }
}
</style>
